<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <h3 class="title">{{ config.app_name }}</h3>
      <el-form-item :prop="config.login.username">
        <el-input
          :name="config.login.username"
          v-model="loginForm[config.login.username]"
          :placeholder="config.login.placeholder"
          type="text"
          auto-complete="on"
          autofocus
        >
          <font-awesome-icon slot="prefix" :icon="config.login.field_icon"/>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          :type="pwdType"
          v-model="loginForm.password"
          auto-complete="on"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <font-awesome-icon slot="prefix" icon="lock"/>
          <font-awesome-icon slot="suffix" :icon="showPwdIcon" class="show-pwd" @click="showPwd"/>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" type="primary" style="width:100%;" @click.native.prevent="handleLogin">
          登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: 'Login',
  data() {
    return {
      loading: false,
      pwdType: 'password',
    }
  },
  computed: {
    ...mapGetters(['config']),
    showPwdIcon() {
      return this.pwdType === 'password'
        ? 'eye-slash'
        : 'eye'
    },
    loginForm() {
      const fields = {
        password: '',
      }
      fields[this.config.login.username] = ''

      return fields
    },
    loginRules() {
      const rules = {
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
      }
      rules[this.config.login.username] = [{ required: true, message: this.usernameMessage, trigger: 'blur' }]

      return rules
    },
    usernameMessage() {
      return '请输入' + this.config.login.label
    },
  },
  methods: {
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('Login', {
              userInfo: this.loginForm,
              config: {
                $form: this.$refs.loginForm,
              },
            })
            .then(() => {
              const redirect = this.$route.query.redirect
              this.$router.push({ path: redirect || '/' })
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
  },
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg: #2d3a4b;
$light_gray: #eee;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 30px;
      color: $light_gray;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
    .el-input__prefix {
      width: 25px;
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}

</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: $bg;
  .login-form {
    position: absolute;
    left: 0;
    right: 0;
    width: 420px;
    padding: 35px 35px 15px 35px;
    margin: 120px auto;
  }
  .title {
    font-size: 26px;
    font-weight: 400;
    color: $light_gray;
    margin: 0px auto 40px auto;
    text-align: center;
  }
  .show-pwd {
    width: 25px;
    font-size: 16px;
    cursor: pointer;
    user-select: none;
  }
}
</style>
